package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"io"
	"log"
	"time"
	hs "xiaohuasheng.cc/user/hello/grpc/HelloService"
)

func main() {
	conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}

	defer conn.Close()

	client := hs.NewHelloServiceClient(conn)
	reply, err := client.Hello(context.Background(), &hs.String{Value: "xhs"})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(reply.GetValue())

	stream, err := client.Channel(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		for i := 0; i < 3; i++ {
			err := stream.Send(&hs.String{Value: fmt.Sprintf("%d say hi", i)})
			if err != nil {
				log.Fatal(err)
			}
			time.Sleep(time.Second)
		}

		stream.Send(&hs.String{Value: "finish"})

	}()

	for {
		reply, err := stream.Recv()
		if err != nil {
			if err == io.EOF {
				fmt.Println("come to here, right?")
				break
			}
			fmt.Println("not in eof")
			log.Fatal(err)
		}
		fmt.Println(reply.GetValue())
	}
}
